package com.wanchuang.hepos.ui.page.mine;

import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CompoundButton;
import android.widget.RadioGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProviders;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.wanchuang.hepos.R;
import com.wanchuang.hepos.bridge.request.MineRequestViewModel;
import com.wanchuang.hepos.bridge.state.mine.CouponViewModel;
import com.wanchuang.hepos.bridge.state.mine.FeedBackViewModel;
import com.wanchuang.hepos.databinding.FragmentCountBinding;
import com.wanchuang.hepos.databinding.FragmentCoupontBinding;
import com.wanchuang.hepos.databinding.FragmentFeedBackBinding;
import com.wanchuang.hepos.proto.MemberOuterClass;
import com.wanchuang.hepos.ui.base.BaseFragment;

/**
 * 文件名 : HomeRootFragment
 * 创建者 : MangoWang
 * 创建日期 : 2019/12/24 16:20
 * 微信 : MangoWaWang
 * 邮箱 : 763482205@qq.com
 * 描述 : TODO
 */
public class CouponFragment extends BaseFragment {
    private FragmentCoupontBinding mBinding;
    private CouponViewModel mViewModel;
    private MineRequestViewModel mRequestViewModel;


    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mViewModel = ViewModelProviders.of(this).get(CouponViewModel.class);
        mRequestViewModel = ViewModelProviders.of(this).get(MineRequestViewModel.class);
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_coupont, container, false);
        mBinding = FragmentCoupontBinding.bind(view);
        mBinding.setClick(new ClickProxy());
        mViewModel.layoutManager.set(new LinearLayoutManager(mActivity));
        mViewModel.layoutManagerTwo.set(new LinearLayoutManager(mActivity));
        mBinding.setVm(mViewModel);
        return view;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);


        mRequestViewModel.getErrorString().observe(this, s -> {
            hideProgress();
            showShortToast(s);
        });
        mRequestViewModel.getResultString().observe(this, s -> {
            hideProgress();
            showShortToast(s);
        });


        mRequestViewModel.get_coupon_list_one().observe(this, couponList -> {
            hideProgress();
            mViewModel.adapter_one.get().addData(couponList);
            int size = couponList.size();
            if (size < 10) {
                mViewModel.adapter_one.get().getLoadMoreModule().loadMoreEnd();
            } else {
                mViewModel.adapter_one.get().getLoadMoreModule().loadMoreComplete();
            }
        });


        mRequestViewModel.get_coupon_list_two().observe(this, couponList -> {
            hideProgress();
            mViewModel.adapter_two.get().addData(couponList);
            int size = couponList.size();
            if (size < 10) {
                mViewModel.adapter_two.get().getLoadMoreModule().loadMoreEnd();
            } else {
                mViewModel.adapter_two.get().getLoadMoreModule().loadMoreComplete();
            }
        });


        mRequestViewModel.getAllTitle().observe(this, allTitle -> {
            mViewModel.allTitle.set(allTitle);
        });
        mRequestViewModel.getAllContent().observe(this, allContent -> {
            mViewModel.allContent.set(allContent);
        });

        mRequestViewModel.getCouponState().observe(this, state -> {
            mViewModel.couponState.set(state);
        });


        showProgress();
        // 未使用
        mRequestViewModel.get_coupon_list("1", mViewModel.page_one);
        // 已使用
        mRequestViewModel.get_coupon_list("2", mViewModel.page_two);


        mRequestViewModel.get_coupon_state();

        mBinding.rgCoupon.setOnCheckedChangeListener((group, checkedId) -> {
            if (checkedId == R.id.rb_one) {
                mBinding.rvOne.setVisibility(View.VISIBLE);
                mBinding.rvTow.setVisibility(View.GONE);
            } else {
                mBinding.rvOne.setVisibility(View.GONE);
                mBinding.rvTow.setVisibility(View.VISIBLE);
            }
        });

        mBinding.swCoupon.setOnCheckedChangeListener((buttonView, isChecked) -> {

            Log.e("isChecked", isChecked + "");
            Log.e("couponState", mViewModel.couponState.get() + "");
            if (isChecked == mViewModel.couponState.get()) return;
            if (isChecked) {
                mRequestViewModel.set_coupon_state("1");
                mViewModel.couponState.set(true);
            } else {
                mViewModel.couponState.set(false);
                mRequestViewModel.set_coupon_state("0");
            }


        });

    }


    public class ClickProxy {
        public void back() {
            nav().navigateUp();
            mSharedViewModel.isHomeLoad.setValue(true);
        }

        public void itemOnClick(int position) {
//            MemberOuterClass.System_log.System_log_list bean = mViewModel.data.get(position);
        }

        public void loadMoreListener() {
            mViewModel.page_one = mViewModel.adapter_one.get().getData().size() / 10 + 1;
            mRequestViewModel.get_coupon_list("1", mViewModel.page_one);
        }

        public void loadMoreListenerTwo() {
            mViewModel.page_two = mViewModel.adapter_two.get().getData().size() / 10 + 1;
            mRequestViewModel.get_coupon_list("2", mViewModel.page_two);
        }
    }
}
